<template>
    <div class="box">
        <header class="header">
            <van-nav-bar title="详情" left-arrow @click-left="$router.go(-1)" />
        </header>
        <main class="content">
            <img style="width:100%" :src="obj.image" alt="">
            <p>{{ obj.title }}</p>
            <p style="color:red"> ￥{{ obj.price }}</p>
        </main>
    </div>
    <footer class="footer">
        <van-action-bar>
            <van-action-bar-icon icon="chat-o" text="客服" dot />
            <van-action-bar-icon icon="cart-o" text="购物车" :badge="store.totalCount" @click="$router.push('/cart')" />
            <van-action-bar-icon icon="shop-o" text="店铺" />
            <van-action-bar-button type="warning" text="加入购物车" @click="addCart" />
            <van-action-bar-button type="danger" text="立即购买" />
        </van-action-bar>
    </footer>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import request from '@/utils/request'
import { useRouter, useRoute } from 'vue-router'
import { useCounterStore } from '@/stores/counter'
const store = useCounterStore()
const route = useRoute()
const router = useRouter()
const obj = ref({})
onMounted(() => {
    request.get('/detail', {
        params: { id: route.params.id }
    }).then(res => {
        if (res.data.code == 200) {
            obj.value = res.data.data
        }
    })
})

const addCart = () => {
    // 判断用户是否登录
    if (localStorage.getItem('token')) {
        // 说明已经登录
        store.addItem(obj.value)
    } else {
        // 说明未登录
        router.push('/login')
    }
}
</script>